package com.studyweb.service.impl;

import com.studyweb.mapper.AchievementMapper;
import com.studyweb.pojo.entity.Achievement;
import com.studyweb.service.AchievementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 成就服务实现类
 * 
 * @author StudyWeb Team
 * @since 1.0.0
 */
@Slf4j
@Service
public class AchievementServiceImpl implements AchievementService {

    @Autowired
    private AchievementMapper achievementMapper;

    @Override
    public Achievement getById(Long achievementId) {
        return achievementMapper.selectById(achievementId);
    }

    @Override
    public List<Achievement> listAll() {
        return achievementMapper.selectAll();
    }

    @Override
    public List<Achievement> listByCategory(String category) {
        return achievementMapper.selectByCategory(category);
    }

    @Override
    public List<Achievement> listByConditionType(String conditionType) {
        return achievementMapper.selectByConditionType(conditionType);
    }

    @Override
    public List<Achievement> listByCondition(String conditionType, Integer conditionValue) {
        return achievementMapper.selectByCondition(conditionType, conditionValue);
    }

    @Override
    public Long add(Achievement achievement) {
        // 设置默认值
        if (achievement.getIsActive() == null) {
            achievement.setIsActive(true);
        }
        if (achievement.getDisplayOrder() == null) {
            achievement.setDisplayOrder(0);
        }
        
        achievementMapper.insert(achievement);
        return achievement.getAchievementId();
    }

    @Override
    public boolean update(Achievement achievement) {
        return achievementMapper.update(achievement) > 0;
    }

    @Override
    public boolean delete(Long achievementId) {
        return achievementMapper.deleteById(achievementId) > 0;
    }
    
    @Override
    public List<Achievement> listByName(String name) {
        return achievementMapper.selectByName(name);
    }
    
    @Override
    public List<Achievement> listByPage(int page, int size, String name, String category) {
        int offset = (page - 1) * size;
        return achievementMapper.selectByPage(offset, size, name, category);
    }
    
    @Override
    public int count(String name, String category) {
        return achievementMapper.count(name, category);
    }
}